package com.logictech.lims.web.back;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ContextHolderUtils;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.logictech.activity.system.vo.LoginUser;
import com.logictech.common.util.data.ExcelUtil;
import com.logictech.common.util.data.SheetData;
import com.logictech.lims.dto.GlasswareDto;
import com.logictech.lims.dto.PreparationInventoryDto;
import com.logictech.lims.dto.ReagentInventoryDto;
import com.logictech.lims.dto.ReagentTypeDto;
import com.logictech.lims.entity.Sourcing;
import com.logictech.lims.service.GlasswareService;
import com.logictech.lims.service.LogService;
import com.logictech.lims.service.PreparationInventoryService;
import com.logictech.lims.service.ReagentInventoryService;
import com.logictech.lims.service.ReagentTypeService;
import com.logictech.lims.service.SourcingService;

/**
 * 描述：</b>GlasswareController<br>
 * 
 * @author guoqi
 * @since：2017年03月23日 13时02分39秒 星期四
 * @version:1.0
 */
@Controller
@RequestMapping("/lims/back/noticeBoard")
public class NoticeBoardController extends BaseController {
	@Autowired
	private ReagentInventoryService reagentInventoryService;

	@Autowired
	private PreparationInventoryService preparationInventoryService;

	@Autowired
	private GlasswareService glasswareService;

	@Autowired
	private ReagentTypeService reagentTypeService;

	@Autowired
	private SourcingService sourcingService;
	
	@Autowired
	private LogService logService;
	
	/**
	 * 列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "listExpire", method = { RequestMethod.GET, RequestMethod.POST })
	public void listExpire(HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		velocityContext.put("pageInfosExpireReagent", SystemTools
				.convertPaginatedList(reagentInventoryService.queryPageList(createReagentPageQuery(pageNo, pageSize))));
		velocityContext.put("pageInfosExpirePreparation", SystemTools.convertPaginatedList(
				preparationInventoryService.queryPageList(createPreparationPageQuery(pageNo, pageSize))));
		velocityContext.put("pageInfosExpireGlassware", SystemTools
				.convertPaginatedList(glasswareService.queryPageList(createGlasswarePageQuery(pageNo, pageSize))));
		velocityContext.put("pageInfosRemindReagentStock", SystemTools.convertPaginatedList(
				reagentTypeService.queryPageList(createReagentTypeServicePageQuery(pageNo, pageSize))));
		velocityContext.put("pageInfosSourcing", SystemTools.convertPaginatedList(
				sourcingService.queryPageList(createSourcingPageQuery(pageNo, pageSize))));
		String viewName = "base/back/main/home.vm";
		ContextHolderUtils.getSession().setAttribute("velocityContext", velocityContext);
		String platform = (String) ContextHolderUtils.getSession().getAttribute("platform");
		velocityContext.put("platform", platform);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
	
	/**
	 * 导出excel
	 * 
	 * @return
	 */
	@RequestMapping(value = "export", method = { RequestMethod.GET, RequestMethod.POST })
	public void list(@ModelAttribute ReagentInventoryDto query, HttpServletResponse response, HttpServletRequest request) throws Exception {
		LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
		logService.doAdd(user.getUserName() + "导出公告栏信息");
		
		// 导出试剂过期信息
		List<ReagentInventoryDto> reagentInventoryDtoList = reagentInventoryService.queryList(createReagentPageQuery());
		LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
		fieldMap.put("barCode", "编号");
		fieldMap.put("reagentType", "试剂名称");
		fieldMap.put("batchNo", "批号");
		fieldMap.put("receivedDate", "接收日期");
		fieldMap.put("opener", "开启人");
		fieldMap.put("openDate", "开启日期");
		fieldMap.put("effectiveDate", "有效日期");
		fieldMap.put("status", "状态");
		
		List<SheetData> sheetDataList = new ArrayList<SheetData>();
		SheetData sheetData = new SheetData();
		sheetData.setList(reagentInventoryDtoList);
		sheetData.setFieldMap(fieldMap);
		sheetData.setSheetName("试剂到期提醒");
		sheetDataList.add(sheetData);
		
		// 导出试液信息
		List<PreparationInventoryDto> preparationInventoryDtoList = preparationInventoryService.queryList(createPreparationPageQuery());
		fieldMap = new LinkedHashMap<String, String>();
		fieldMap.put("barCode", "编号");
		fieldMap.put("preparationType", "试液名称");
		fieldMap.put("configurationDate", "配制日期");
		fieldMap.put("effectiveDate", "有效日期");
		fieldMap.put("status", "状态");
		sheetData = new SheetData();
		sheetData.setList(preparationInventoryDtoList);
		sheetData.setFieldMap(fieldMap);
		sheetData.setSheetName("试液到期提醒");
		sheetDataList.add(sheetData);
		
		List<GlasswareDto> glasswareDtoList = glasswareService.queryList(createGlasswarePageQuery());
		fieldMap = new LinkedHashMap<String, String>();
		fieldMap.put("no", "编号");
		fieldMap.put("type", "类型");
		fieldMap.put("size", "规格(ml)");
		fieldMap.put("status", "状态");
		fieldMap.put("createTimeExp", "入库时间");
		fieldMap.put("checkTimeExp", "校验完成时间");
		fieldMap.put("scrapTimeExp", "报废时间");
		fieldMap.put("scrapReason", "报废原因");
		sheetData = new SheetData();
		sheetData.setList(glasswareDtoList);
		sheetData.setFieldMap(fieldMap);
		sheetData.setSheetName("玻璃仪器校验提醒");
		sheetDataList.add(sheetData);
		
		List<ReagentTypeDto> reagentTypeDtoList = reagentTypeService.queryList(createReagentTypeServicePageQuery());
		fieldMap = new LinkedHashMap<String, String>();
		fieldMap.put("englishName", "英文名称");
		fieldMap.put("chineseName", "中文名称");
		fieldMap.put("positionNo", "位置");
		fieldMap.put("locationNo", "库位");
		fieldMap.put("storageCondition", "存放条件");
		fieldMap.put("casNo", "CAS序列号");
		fieldMap.put("size", "一般规格(ml)");
		fieldMap.put("hazardPrecautions", "危害注意事项");
		fieldMap.put("protectiveCondition", "防护条件");
		fieldMap.put("alarmStorage", "警戒值");
		fieldMap.put("storage", "库存量");
		sheetData = new SheetData();
		sheetData.setList(reagentTypeDtoList);
		sheetData.setFieldMap(fieldMap);
		sheetData.setSheetName("试剂库存报警");
		sheetDataList.add(sheetData);
		
		List<Sourcing> sourcingList = sourcingService.queryList(createSourcingPageQuery());
		fieldMap = new LinkedHashMap<String, String>();
		fieldMap.put("requirement", "采购需求");
		fieldMap.put("level", "级别");
		fieldMap.put("quantity", "数量");
		fieldMap.put("requestArrivalDate", "要求到货日期");
		fieldMap.put("status", "状态");
		fieldMap.put("createUser", "提出人");
		fieldMap.put("createTime", "提出日期");
		
		sheetData = new SheetData();
		sheetData.setList(sourcingList);
		sheetData.setFieldMap(fieldMap);
		sheetData.setSheetName("采购需求");
		sheetDataList.add(sheetData);
		
		ExcelUtil.listsToExcel(sheetDataList, 65535, response);
	}

	@RequestMapping(value = "listExpireReagent", method = { RequestMethod.GET, RequestMethod.POST })
	public void listExpireReagent(HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "formSubmit1PageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "formSubmit1PageSize", defaultValue = "10") int pageSize)
					throws Exception {
		VelocityContext velocityContext = (VelocityContext) ContextHolderUtils.getSession()
				.getAttribute("velocityContext");
		velocityContext.put("pageInfosExpireReagent", SystemTools
				.convertPaginatedList(reagentInventoryService.queryPageList(createReagentPageQuery(pageNo, pageSize))));
		String viewName = "base/back/main/home.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = "listExpirePreparation", method = { RequestMethod.GET, RequestMethod.POST })
	public void listExpirePreparation(HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "formSubmit2PageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "formSubmit2PageSize", defaultValue = "10") int pageSize)
					throws Exception {
		VelocityContext velocityContext = (VelocityContext) ContextHolderUtils.getSession()
				.getAttribute("velocityContext");
		velocityContext.put("pageInfosExpirePreparation", SystemTools.convertPaginatedList(
				preparationInventoryService.queryPageList(createPreparationPageQuery(pageNo, pageSize))));
		String viewName = "base/back/main/home.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = "listExpireGlassware", method = { RequestMethod.GET, RequestMethod.POST })
	public void listExpireGlassware(HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "formSubmit3PageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "formSubmit3PageSize", defaultValue = "10") int pageSize)
					throws Exception {
		VelocityContext velocityContext = (VelocityContext) ContextHolderUtils.getSession()
				.getAttribute("velocityContext");
		velocityContext.put("pageInfosExpireGlassware", SystemTools
				.convertPaginatedList(glasswareService.queryPageList(createGlasswarePageQuery(pageNo, pageSize))));
		String viewName = "base/back/main/home.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "listRemindReagentStock", method = { RequestMethod.GET, RequestMethod.POST })
	public void listAlarmStorageReagent(HttpServletResponse response,HttpServletRequest request,
			@RequestParam(required = false, value = "formSubmit4PageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "formSubmit4PageSize", defaultValue = "10") int pageSize)
					throws Exception {
		VelocityContext velocityContext = (VelocityContext) ContextHolderUtils.getSession()
				.getAttribute("velocityContext");
		velocityContext.put("pageInfosRemindReagentStock", SystemTools.convertPaginatedList(
				reagentTypeService.queryPageList(createReagentTypeServicePageQuery(pageNo, pageSize))));
		String viewName = "base/back/main/home.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
	
	/**
	 * 列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "listSourcing", method = { RequestMethod.GET, RequestMethod.POST })
	public void listSourcing(HttpServletResponse response,HttpServletRequest request,
			@RequestParam(required = false, value = "formSubmit5PageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "formSubmit5PageSize", defaultValue = "10") int pageSize)
					throws Exception {
		VelocityContext velocityContext = (VelocityContext) ContextHolderUtils.getSession()
				.getAttribute("velocityContext");
		velocityContext.put("pageInfosSourcing", SystemTools.convertPaginatedList(
				sourcingService.queryPageList(createSourcingPageQuery(pageNo, pageSize))));
		String viewName = "base/back/main/home.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	private PageQuery<ReagentInventoryDto> createReagentPageQuery(int pageNo, int pageSize) {
		PageQuery<ReagentInventoryDto> pageQuery = new PageQuery<ReagentInventoryDto>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		ReagentInventoryDto query = new ReagentInventoryDto();
		// expire不为空时，查询过期试剂数据
		query.setExpire("1");
		pageQuery.setQuery(query);
		return pageQuery;
	}
	
	private PageQuery<ReagentInventoryDto> createReagentPageQuery() {
		PageQuery<ReagentInventoryDto> pageQuery = new PageQuery<ReagentInventoryDto>();
		ReagentInventoryDto query = new ReagentInventoryDto();
		// expire不为空时，查询过期试剂数据
		query.setExpire("1");
		pageQuery.setQuery(query);
		return pageQuery;
	}

	private PageQuery<PreparationInventoryDto> createPreparationPageQuery(int pageNo, int pageSize) {
		PageQuery<PreparationInventoryDto> pageQuery = new PageQuery<PreparationInventoryDto>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		PreparationInventoryDto query = new PreparationInventoryDto();
		// expire不为空时，查询过期试液数据
		query.setExpire("1");
		pageQuery.setQuery(query);
		return pageQuery;
	}
	
	private PageQuery<PreparationInventoryDto> createPreparationPageQuery() {
		PageQuery<PreparationInventoryDto> pageQuery = new PageQuery<PreparationInventoryDto>();
		PreparationInventoryDto query = new PreparationInventoryDto();
		// expire不为空时，查询过期试液数据
		query.setExpire("1");
		pageQuery.setQuery(query);
		return pageQuery;
	}

	private PageQuery<ReagentTypeDto> createReagentTypeServicePageQuery(int pageNo, int pageSize) {
		PageQuery<ReagentTypeDto> pageQuery = new PageQuery<ReagentTypeDto>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		ReagentTypeDto query = new ReagentTypeDto();
		// expire不为空时，查询过期试液数据
		query.setRemindStock("1");
		pageQuery.setQuery(query);
		return pageQuery;
	}
	
	private PageQuery<ReagentTypeDto> createReagentTypeServicePageQuery() {
		PageQuery<ReagentTypeDto> pageQuery = new PageQuery<ReagentTypeDto>();
		ReagentTypeDto query = new ReagentTypeDto();
		// expire不为空时，查询过期试液数据
		query.setRemindStock("1");
		pageQuery.setQuery(query);
		return pageQuery;
	}

	private PageQuery<GlasswareDto> createGlasswarePageQuery(int pageNo, int pageSize) {
		PageQuery<GlasswareDto> pageQuery = new PageQuery<GlasswareDto>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		GlasswareDto query = new GlasswareDto();
		// expire不为空时，查询过期试液数据
		query.setExpire("1");
		pageQuery.setQuery(query);
		return pageQuery;
	}
	
	private PageQuery<GlasswareDto> createGlasswarePageQuery() {
		PageQuery<GlasswareDto> pageQuery = new PageQuery<GlasswareDto>();
		GlasswareDto query = new GlasswareDto();
		// expire不为空时，查询过期试液数据
		query.setExpire("1");
		pageQuery.setQuery(query);
		return pageQuery;
	}
	
	private PageQuery<Sourcing> createSourcingPageQuery(int pageNo, int pageSize) {
		PageQuery<Sourcing> pageQuery = new PageQuery<Sourcing>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		Sourcing query = new Sourcing();
		// 未到货
		query.setStatus("0");
		pageQuery.setQuery(query);
		return pageQuery;
	}
	
	private PageQuery<Sourcing> createSourcingPageQuery() {
		PageQuery<Sourcing> pageQuery = new PageQuery<Sourcing>();
		Sourcing query = new Sourcing();
		// 未到货
		query.setStatus("0");
		pageQuery.setQuery(query);
		return pageQuery;
	}
}
